<?php

ini_set('memory_limit','16M');

require_once( '/home/cyberpower678/Peachy/Init.php' );

$site = Peachy::newWiki( "cyberbot i" );

$site->set_runpage("User:Cyberbot I/Run/Obama");

$request = $site->get_http()->get( 'http://www.pollingreport.com/obama_job.htm' );

$body = explode('<table border="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="700" cellpadding="0" id="AutoNumber27">',$body);
$body = explode('</table>',$body[1]);
$body = $body[0];

preg_match_all('/'.
                              '\<font face="Verdana" size="2" color="#666666"\>(.*?)\<\/font\>\<\/span\>\<\/td\>\s*\<td width="88" align="center"\>\<span lang="en-us"\>\s*\<font face="Verdana" size="2" color="#666666"\>(\d{1,3})\<\/font\>\<\/span\>\<\/td\>\s*\<td width="89" align="center"\>\<span lang="en-us"\>\s*\<font face="Verdana" size="2" color="#666666"\>(\d{1,3})\<\/font\>\<\/span\>\<\/td\>/i',$request, $m);

$dates = array_reverse($m[1]);
$sup = $m[2];
$opp = $m[3];

$approve = array();
$disapprove = array();
$neutral = array();
foreach( $dates as $key => $value ) {
    $date = preg_match('/^(\d*)\/(.*?)\/(\d*)$/',$value,$r);
    $d = $r[2];
    $m = $r[1];
    $y = $r[3];
    $d = preg_replace('/(\d*)\s?-\s?\d*/','\1',$d);
    $d = explode('/',$d);
    $d = $d[0];
    if( $d % 3 != 0 ) { continue; }
    $diff = (strtotime(date("Y-m-d"))-strtotime("20$y-$m-$d")) / (60 * 60 * 24);
    if( $diff > 700 || $diff < 0 ) { continue; }
    $approve[] = $diff.','.($sup[$key]*10);
    $disapprove[] = $diff.','.($opp[$key]*10);
    echo $diff.','.($opp[$key])."\n";
    $neutral[] = $diff.','.((100 - $sup[$key] - $opp[$key])*10);
}

$code = file_get_contents('/home/cyberpower678/public_html/Barack_Obama_approval_ratings.svg');
$code = preg_replace('/\<polyline stroke="#4A7EBB" points="(.*?)"\/\>/ms','<polyline stroke="#4A7EBB" points="'.implode(" \n",$approve).'"/>',$code);
$code = preg_replace('/\<polyline stroke="#BE4B48" points="(.*?)"\/\>/ms','<polyline stroke="#BE4B48" points="'.implode(" \n",$disapprove).'"/>',$code);
$code = preg_replace('/\<polyline stroke="#98B954" points="(.*?)"\/\>/ms','<polyline stroke="#98B954" points="'.implode(" \n",$neutral).'"/>',$code);

file_put_contents('/home/cyberpower678/public_html/Barack_Obama_approval_ratings.svg', $code);

$image = initImage( "File:Barack_Obama_approval_ratings.svg" );

$image->upload('/home/cyberpower678/public_html/Barack_Obama_approval_ratings.svg','','Automated upload of graph');

?>

